netfilter: nf_tables: fix chain binding transaction logic
authorPablo Neira Ayuso <pablo@netfilter.org>
Thu, 13 Jul 2023 08:48:52 +0000 (10:48 +0200)
committerSalvatore Bonaccorso <carnil@debian.org>
Thu, 27 Jul 2023 22:17:15 +0000 (23:17 +0100)
commit5f96d976559ddb8d3f296f7be7deab90852d4f45
tree4297c8340b292603b36d1808c793066ee23a194c
parent87c349a72db3c900bcec6e072d3b1170c1c6e648
netfilter: nf_tables: fix chain binding transaction logic

Origin: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit?id=d53c295c1f43b7460d28ba0f0f98a602084fdcb6
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2023-3610

[ Upstream commit 4bedf9eee016286c835e3d8fa981ddece5338795 ]

Add bound flag to rule and chain transactions as in 6a0a8d10a366
("netfilter: nf_tables: use-after-free in failing rule with bound set")
to skip them in case that the chain is already bound from the abort
path.

This patch fixes an imbalance in the chain use refcnt that triggers a
WARN_ON on the table and chain destroy path.

This patch also disallows nested chain bindings, which is not
supported from userspace.

The logic to deal with chain binding in nft_data_hold() and
nft_data_release() is not correct. The NFT_TRANS_PREPARE state needs a
special handling in case a chain is bound but next expressions in the
same rule fail to initialize as described by 1240eb93f061 ("netfilter:
nf_tables: incorrect error path handling with NFT_MSG_NEWRULE").

The chain is left bound if rule construction fails, so the objects
stored in this chain (and the chain itself) are released by the
transaction records from the abort path, follow up patch ("netfilter:
nf_tables: add NFT_TRANS_PREPARE_ERROR to deal with bound set/chain")
completes this error handling.

When deleting an existing rule, chain bound flag is set off so the
rule expression .destroy path releases the objects.

Fixes: d0e2c7de92c7 ("netfilter: nf_tables: add NFT_CHAIN_BINDING")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[bwh: Backported to 5.10:
 - Use net->nft instead of net_generic(net, nf_tables_net_id)
 - Adjust context]

Gbp-Pq: Topic bugfix/all
Gbp-Pq: Name netfilter-nf_tables-fix-chain-binding-transaction-lo.patch
include/net/netfilter/nf_tables.h
net/netfilter/nf_tables_api.c
net/netfilter/nft_immediate.c